<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件使用的细节</title>
    <script src="./vue.js"></script>
</head>
<body>
<div id="app">
    <!-- @change="handleChange" 监听子组件发射出来的change事件-->
    <counter ref="counter1" @change="handleChange"></counter>
    <counter ref="counter2" @change="handleChange"></counter>
    <!--计算两个计数器的和-->
    <div>
        {{total}}
    </div>
</div>
<script>
    //计数器，点击+1
    Vue.component('counter', {
        data: function () {
            return {
                number: 0
            }
        },
        template: '<div @click="handleClick">{{number}}</div>',
        methods: {
            handleClick: function () {
                this.number++;
                // 发布订阅模型，向父组件发射一个change事件
                this.$emit('change')
            }
        }
    })
    var app = new Vue({
        el: '#app',
        data: {
            total: 0
        },
        methods: {
            // 监听子组件发射出的change事件，并计算两个计算器的和
            handleChange: function () {
                console.log('counter1: ' + this.$refs.counter1.number);
                console.log('counter2: ' + this.$refs.counter2.number);
                this.total = this.$refs.counter1.number + this.$refs.counter2.number
            }
        }
    });
</script>
</body>
</html>